package cn.kailangtu.dataspot.activify;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import cn.kailangtu.commonevents.eventEntity.KeyboardNumberActionEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardNumberInputEvent;
import cn.kailangtu.dataspot.data.consts.KeyboardAction;
import cn.kailangtu.dataspot.data.context.KeyboardContext;
import cn.kailangtu.dataspot.databinding.FragmentKeyboardNumberBinding;

public class KeyboardNumberFragment extends KeyboardBaseFragment {

    private FragmentKeyboardNumberBinding binding;

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentKeyboardNumberBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    private void initListener(){
        binding.btnKeyboard0.setOnClickListener(keybaodListener);
        binding.btnKeyboard1.setOnClickListener(keybaodListener);
        binding.btnKeyboard2.setOnClickListener(keybaodListener);
        binding.btnKeyboard3.setOnClickListener(keybaodListener);
        binding.btnKeyboard4.setOnClickListener(keybaodListener);
        binding.btnKeyboard5.setOnClickListener(keybaodListener);
        binding.btnKeyboard6.setOnClickListener(keybaodListener);
        binding.btnKeyboard7.setOnClickListener(keybaodListener);
        binding.btnKeyboard8.setOnClickListener(keybaodListener);
        binding.btnKeyboard9.setOnClickListener(keybaodListener);
        binding.btnKeyboardDot.setOnClickListener(keybaodListener);
        binding.btnKeyboardDelete.setOnClickListener(keybaodListener);
        binding.btnKeyboardOk.setOnClickListener(keybaodListener);
        binding.btnKeyboardCancel.setOnClickListener(keybaodListener);
    }

    private View.OnClickListener keybaodListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView current = (TextView)view;
            KeyboardContext.setSelectedView(current);
            KeyboardContext.entry();
            binding.tvKeyboardResult.setText(KeyboardContext.result);
        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(KeyboardNumberActionEvent event){
        keyboard(event.getActionValue(),event.getActionData());
    }

    private void keyboard(int action,String data ){
        switch (action) {
            case KeyboardAction.OPEN:
                binding.tvKeyboardResult.setText(data);
                KeyboardContext.result = data;
                KeyboardContext.isFirstEdit = true;
                break;
            case KeyboardAction.ENTRY:
                KeyboardContext.entry();
                binding.tvKeyboardResult.setText(KeyboardContext.result);
                break;
            case KeyboardAction.LEFT:

                if(KeyboardContext.getSelectedView() == null)
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard1);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard1))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboardCancel);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboardCancel))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboardOk);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboardOk))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboardDelete);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboardDelete))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboardDot);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboardDot))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard0);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard0))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard9);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard9))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard8);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard8))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard7);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard7))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard6);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard6))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard5);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard5))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard4);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard4))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard3);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard3))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard2);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard2))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard1);
                }
                else
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard1);
                }
                break;
            case KeyboardAction.RIGHT:
                if(KeyboardContext.getSelectedView() == null)
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard1);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard1))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard2);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard2))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard3);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard3))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard4);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard4))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard5);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard5))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard6);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard6))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard7);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard7))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard8);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard8))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard9);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard9))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard0);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboard0))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboardDot);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboardDot))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboardDelete);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboardDelete))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboardOk);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboardOk))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboardCancel);
                }
                else if(KeyboardContext.getSelectedView().equals(binding.btnKeyboardCancel))
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard1);
                }
                else
                {
                    KeyboardContext.setSelectedView(binding.btnKeyboard1);
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(KeyboardNumberInputEvent event){
        String key =  event.getInputKey();
        switch(key)
        {
            case "0":
                binding.btnKeyboard0.callOnClick();
                break;
            case "1":
                binding.btnKeyboard1.callOnClick();
                break;
            case "2":
                binding.btnKeyboard2.callOnClick();
                break;
            case "3":
                binding.btnKeyboard3.callOnClick();
                break;
            case "4":
                binding.btnKeyboard4.callOnClick();
                break;
            case "5":
                binding.btnKeyboard5.callOnClick();
                break;
            case "6":
                binding.btnKeyboard6.callOnClick();
                break;
            case "7":
                binding.btnKeyboard7.callOnClick();
                break;
            case "8":
                binding.btnKeyboard8.callOnClick();
                break;
            case "9":
                binding.btnKeyboard9.callOnClick();
                break;
            case "BACK":
                binding.btnKeyboardDelete.callOnClick();
                break;
            case ".":
                binding.btnKeyboardDot.callOnClick();
                break;
        }
    }
}
